<template>
  <div v-loading="infoLoading">
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="98%"
      top="20px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      append-to-body
    >
      <el-form ref="postForm" :model="postForm" class="form-container" label-width="100px" :rules="rules">
        <el-form-item key="dbdw" prop="dbdw" label="单位">
          {{ postForm.dbdw }}
        </el-form-item>
        <el-form-item key="bdbdw" prop="bdbdw" label="被督办单位">
          {{ postForm.bdbdw }}
        </el-form-item>
        <el-form-item v-if="postForm.dbsj" key="dbsj" prop="dbsj" label="时间">
          {{ postForm.dbsj }}
        </el-form-item>
        <el-form-item key="dbqx" prop="dbqx" label="办理期限">
          <div v-if="operation==='add'"><el-input-number v-model="postForm.dbqx" style="width:200px" /> 天</div>
          <span v-else>{{ postForm.dbqx?postForm.dbqx+'天':'无' }}</span>
        </el-form-item>
        <el-form-item key="dbsx" prop="dbsx" label="事项">
          <el-input v-if="operation==='add'" v-model="postForm.dbsx" type="textarea" style="width:90%" />
          <span v-else>{{ postForm.dbsx }}</span>
        </el-form-item>
        <el-form-item v-if="operation!=='add'&&postForm.blsj" key="blsj" prop="blsj" label="办结时间">
          {{ postForm.blsj }}
        </el-form-item>
        <el-form-item v-if="operation!=='add'" key="bljg" prop="bljg" label="办理结果">
          <el-input v-if="operation==='feedback'" v-model="postForm.bljg" type="textarea" style="width:90%" />
          <span v-else>{{ postForm.bljg }}</span>
        </el-form-item>
        <el-form-item key="submit" style="margin:10px 0">
          <el-button type="primary" :loading="loading" @click="submitForm">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <h2>工作进展情况</h2>
    <el-table
      ref="processTable"
      :data="processList"
      border
      fit
      highlight-current-row
      style="width: 100%"
      :loading="processListLoading"
    >
      <el-table-column align="center" type="index" label="序号" width="100" />
      <el-table-column align="center" prop="dbdw" label="单位" width="150" />
      <el-table-column align="center" prop="bdbdw" label="被督办单位" width="150" />
      <el-table-column align="center" prop="dbsj" label="时间" width="100" />
      <el-table-column align="center" prop="dbqx" label="办理期限" width="100" />
      <el-table-column prop="dbsx" label="事项" min-width="200" show-overflow-tooltip />
      <el-table-column align="center" prop="blsj" label="办结时间" width="100" />
      <el-table-column prop="bljg" label="办理结果" min-width="200" show-overflow-tooltip />
      <el-table-column align="center" width="100">
        <template slot="header">
          <el-button
            v-if="canAdd"
            type="success"
            @click="add"
          >新增</el-button>
        </template>
        <template slot-scope="scope">
          <!-- <el-button
            type="primary"
            @click="handleEdit(scope.row.id)"
          >修改</el-button>
          <el-button
            type="danger"
            @click="handleDelete(scope.row.id)"
          >删除</el-button> -->
          <el-button
            v-if="checkCanFeedback(scope.row.bdbdw)&&!scope.row.blsj"
            type="success"
            @click="feedback(scope.row)"
          >办理</el-button>
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>

<script>
const defaultForm = {
  dbdw: '',
  bdbdw: '',
  bdbdwid: 0,
  dbsj: '',
  dbqx: '',
  dbsx: '',
  blsj: '',
  bljg: '',
  id: null
}
export default {
  name: 'Supervice',
  props: {
    cfajbh: {
      type: String,
      default: ''
    },
    timestamp: {
      type: Number,
      default: 0
    },
    businessTable: {
      type: String,
      default: ''
    }
  },
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value === null || value === undefined || value === '') {
        // this.$message({
        //   message: '请将表单填写完整',
        //   type: 'error'
        // })
        callback(new Error('必填项'))
      } else {
        callback()
      }
    }
    return {
      operation: '',
      canAdd: false,
      canFeedback: false,
      dialogVisible: false,
      dialogTitle: '新增',
      myHeaders: {},
      infoLoading: false,
      postForm: Object.assign({}, defaultForm),
      loading: false,
      rules: {
        dbsx: [{ validator: validateRequire }]
      },
      tempRoute: {},
      processListLoading: false,
      processList: [
        // { time: '第一次', unit1: '处非办', unit2: '公安局', date: '2021-05-10', limit: '10天', item: '尽快办理', result: '已办结' },
        // { time: '第二次', unit1: '处非办', unit2: '公安局', date: '2021-05-20', limit: '10天', item: '尽快办理', result: '已办结' }
      ]
    }
  },
  watch: {
    cfajbh: {
      handler(val, oldVal) {
        if (this.$refs['postForm']) {
          this.$refs['postForm'].resetFields()
        }
        if (this.cfajbh) {
          this.getData()
        }
      },
      immediate: true
    }
  },
  mounted() {
    this.myHeaders = {
      Authorization: 'Bearer ' + localStorage.getItem('token')
    }
    if (JSON.parse(localStorage.getItem('user')).unitName === '处非办' || JSON.parse(localStorage.getItem('user')).unitName === '督查室' || JSON.parse(localStorage.getItem('user')).unitName === '政法委') {
      this.canAdd = true
    }
  },
  created() {
    this.tempRoute = Object.assign({}, this.$route)
  },
  methods: {
    checkCanFeedback(unitName) {
      return JSON.parse(localStorage.getItem('user')).unitName === unitName
    },
    add0(m) {
      return m < 10 ? '0' + m : m
    },
    handleDelete() {
    },
    handleEdit(id) {
      this.dialogVisible = true
      this.dialogTitle = '修改'
    },
    feedback(item) {
      this.operation = 'feedback'
      this.postForm = item
      this.dialogVisible = true
      this.dialogTitle = '办理'
    },
    add() {
      this.operation = 'add'
      this.postForm = Object.assign({}, defaultForm)
      this.postForm.dbdw = JSON.parse(localStorage.getItem('user')).unitName
      switch (this.businessTable) {
        case 'bus_ga':
          this.postForm.bdbdw = '公安'
          this.postForm.bdbdwid = 2
          break
        case 'bus_hcdj':
          this.postForm.bdbdw = '公安'
          this.postForm.bdbdwid = 2
          break
        case 'bus_sfsj':
          this.postForm.bdbdw = '公安'
          this.postForm.bdbdwid = 2
          break
        case 'bus_ysscqs':
          this.postForm.bdbdw = '公安'
          this.postForm.bdbdwid = 2
          break
        case 'bus_cxa':
          this.postForm.bdbdw = '公安'
          this.postForm.bdbdwid = 2
          break
        case 'bus_qs':
          this.postForm.bdbdw = '检察院'
          this.postForm.bdbdwid = 3
          break
        case 'bus_sp':
          this.postForm.bdbdw = '法院'
          this.postForm.bdbdwid = 4
          break
        case 'bus_zx':
          this.postForm.bdbdw = '法院'
          this.postForm.bdbdwid = 4
          break
      }
      this.dialogVisible = true
      this.dialogTitle = '新增'
    },
    getData() {
      this.processListLoading = true
      const para = {
        businessTable: this.businessTable,
        cfajbh: this.cfajbh,
        pageNum: 1,
        pageSize: 1000
      }
      this.$http
        .post('api/busSubDb/queryByPage', para)
        .then(response => {
          this.processListLoading = false
          if (response.data.success === true) {
            this.processList = response.data.result.records
          } else {
            console.log('获取失败:' + response.data.message)
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
          this.processListLoading = false
        })
    },
    submitForm(flag) {
      const day = new Date()
      day.setTime(day.getTime())
      this.postForm.dbsj = day.getFullYear() + '-' + this.add0(day.getMonth() + 1) + '-' + this.add0(day.getDate())
      this.$refs.postForm.validate(valid => {
        if (valid) {
          let para = {}
          let url = ''
          if (this.operation === 'add') {
            para = {
              businessTable: this.businessTable,
              cfajbh: this.cfajbh,
              dbsj: this.postForm.dbsj,
              dbqx: this.postForm.dbqx,
              dbsx: this.postForm.dbsx,
              bdbdw: this.postForm.bdbdw,
              bdbdwid: this.postForm.bdbdwid
            }
            url = '/api/busSubDb/saveDb'
          } else if (this.operation === 'feedback') {
            para = {
              id: this.postForm.id,
              bljg: this.postForm.bljg
            }
            url = '/api/busSubDb/saveFeedback'
          }
          this.loading = true
          this.$http
            .post(url, para)
            .then(response => {
              this.loading = false
              if (response.data.success === true) {
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
                this.dialogVisible = false
                this.getData()
                this.postForm = Object.assign({}, defaultForm)
                this.loading = false
              } else {
                console.log('操作失败:' + response.data.message)
                this.$message({
                  message: '操作失败:' + response.data.message,
                  type: 'error'
                })
              }
            })
            .catch(error => {
              console.log(error)
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
